package com.thinkfit.jy.activity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.common.lib.activity.BaseActivity;
import com.common.lib.bean.VideoListBean;
import com.common.lib.constant.EventBusEvent;
import com.common.lib.mvp.contract.EmptyContract;
import com.common.lib.mvp.presenter.EmptyPresenter;
import com.thinkfit.jy.R;
import com.thinkfit.jy.adapter.CreateTrainPlanAdapter;
import com.thinkfit.jy.adapter.TrainPatternAdapter;
import com.thinkfit.jy.bean.CreateTrainItemBean;
import com.thinkfit.jy.dialog.CreateTrainPopupWindow;
import com.thinkfit.jy.dialog.TrainGeneralSituationPopupWindow;
import com.thinkfit.jy.serialport.SerialPortManager;
import com.thinkfit.jy.utils.LogUtils;
import com.thinkfit.jy.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import xyz.doikki.videocontroller.StandardVideoController;
import xyz.doikki.videoplayer.player.AndroidMediaPlayerFactory;
import xyz.doikki.videoplayer.player.VideoView;

public class CreateTrainPlanActivity extends BaseActivity<EmptyContract.Presenter> implements EmptyContract.View{

    LinearLayout llSportBack;
    RecyclerView rvTrainPlan;
    TextView tvAddSection,tvSave,tvArea,tvLanguage,tvUnit;

    CreateTrainPlanAdapter createTrainPlanAdapter;

    ArrayList<View> itemList = new ArrayList<>();

    protected int getLayoutId() {
        return R.layout.activity_create_train_plan;
    }

    @NonNull
    @Override
    protected EmptyContract.Presenter onCreatePresenter() {
        return new EmptyPresenter(this);
    }
    @Override
    protected void onCreated(@Nullable Bundle savedInstanceState) {
        init();
    }

    public void init(){
        llSportBack = findViewById(R.id.ll_sport_back);
        tvAddSection = findViewById(R.id.tv_add_section);
        tvSave = findViewById(R.id.tv_save);
        rvTrainPlan = findViewById(R.id.rv_train_plan);

        tvArea = findViewById(R.id.tv_area);
        tvLanguage = findViewById(R.id.tv_language);
        tvUnit = findViewById(R.id.tv_unit);

        initRecycler();

        llSportBack.setOnClickListener(this);
        tvAddSection.setOnClickListener(this);
        tvSave.setOnClickListener(this);

        Utils.initRegion(tvArea,tvLanguage,tvUnit);
    }

    public void initRecycler(){
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvTrainPlan.setLayoutManager(linearLayoutManager);
        rvTrainPlan.setAdapter(getAdapter());
    }

    public CreateTrainPlanAdapter getAdapter() {
        if(createTrainPlanAdapter == null){
            createTrainPlanAdapter = new CreateTrainPlanAdapter(this);
        }
//        createTrainPlanAdapter.setOnItemClickListener(new OnItemClickListener() {
//            @Override
//            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
//
//            }
//        });
        createTrainPlanAdapter.setOnClick(new CreateTrainPlanAdapter.OnClick() {
            @Override
            public void onClick(View view, int position, CreateTrainItemBean bean) {
                bean.setPosition(position);
                new CreateTrainPopupWindow(CreateTrainPlanActivity.this,bean).show(CreateTrainPlanActivity.this);
            }
        });
        return createTrainPlanAdapter;
    }

    @Override
    public void onReceive(@NonNull HashMap<String, Object> map) {
        if (map.containsKey(EventBusEvent.CREATE_TRAIN_INFO)){
            CreateTrainItemBean bean = (CreateTrainItemBean) map.get(EventBusEvent.CREATE_TRAIN_INFO);
            LogUtils.getInstance().l(LogUtils.HINT,"xxxxxxxxxxxxxxxxxxxxxxxxxxxxx  Position : "+bean.getPosition());
            LogUtils.getInstance().l(LogUtils.HINT,"xxxxxxxxxxxxxxxxxxxxxxxxxxxxx  CreateTrainItemBean : "+bean.toString());
            getAdapter().getData().get(bean.getPosition()).setSlop(bean.getSlop());
            getAdapter().getData().get(bean.getPosition()).setSpeed(bean.getSpeed());
            getAdapter().getData().get(bean.getPosition()).setTime(bean.getTime());
            getAdapter().notifyItemChanged(bean.getPosition());
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public void addSection(){
        getAdapter().addData(new CreateTrainItemBean());
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_sport_back:
                finish();
                break;
            case R.id.tv_add_section:
                addSection();
                break;
            case R.id.tv_save:
                TrainGeneralSituationPopupWindow trainGeneralSituationPopupWindow = new TrainGeneralSituationPopupWindow(getContext(), getAdapter().getData());
                trainGeneralSituationPopupWindow.show(this);
                break;

        }
    }
}
